package com.fzh.controller.back;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fzh.pojo.entity.Message;
import com.fzh.pojo.entity.User;
import com.fzh.pojo.utils.PageUtil;
import com.fzh.pojo.utils.ResponeMessageUtil;
import com.fzh.service.MessageService;
import com.fzh.service.UserService;

@Controller
@RequestMapping("/back/")
public class MessageController {

	@Autowired
	private MessageService messageService;
	
	@Autowired
	private UserService userService;
	
	
	/**
	 * 查询出所有的未回复的留言，用于在顶部铃铛提示
	 * @return
	 */
	@RequestMapping("/queryAllMessageIsAnswer")
	@ResponseBody
	public List<Message> queryAllMessageIsAnswer(){
		return messageService.queryAllMessageIsAnswer();
	}
	
	/**
	 * 查询出所有的留言内容
	 * @param page
	 * @param limit
	 * @param content
	 * @param isAnswer
	 * @param isPublic
	 * @return
	 */
	@RequestMapping("/queryAllMessage")
	@ResponseBody
	public ResponeMessageUtil queryAllMessage(Integer page,Integer limit,String content,String isAnswer,String isPublic) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		return ResponeMessageUtil.ok(messageService.queryMessageCounts(content,isAnswer,isPublic), messageService.queryAllMessage(setValue,limit,content,isAnswer,isPublic));
	}
	
	/**
	 * 查询出用户的信息用于展示到留言页面的下拉框内
	 * @return
	 */
	@RequestMapping("/queryAllUserWitchMessageSelect")
	@ResponseBody
	public List<User> queryAllUserWitchMessageSelect(){
		return userService.queryAllUserWitchOrderIndoSelect();
	}
	
	/**
	 * 添加一个留言
	 * @param message
	 * @return
	 */
	@RequestMapping("/addMessage")
	@ResponseBody
	public Boolean addMessage(Message message) {
		return messageService.addMessage(message);
	}
	
	/**
	 * 编辑留言信息根据id
	 * @param isPublicStatus
	 * @param message
	 * @return
	 */
	@RequestMapping("/editMessageById")
	@ResponseBody
	public Boolean editMessageById(String isPublicStatus,Message message) {
		//如开关开启代表已回复，给isPublic赋值为1
		if(isPublicStatus != null && isPublicStatus != "" && isPublicStatus.equals("on")) {
			message.setIsPublic(1);
		}else {
			message.setIsPublic(0);
		}
		//当编辑后代表已回复设为1
		message.setIsAnswer(1);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		message.setAnswerTime(dateFormat.format(new Date()));
		return messageService.editMessageById(message);
	}
	
	/**
	 * 批量的删除留言信息根据ids
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delAllMessageByIds")
	@ResponseBody
	public Boolean delAllMessageByIds(@RequestParam("ids") String ids) {
		return messageService.delAllMessageByIds(ids);
		
	}
	
	/**
	 * 根据id删除单个的留言信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/delMessageById/{id}")
	@ResponseBody
	public Boolean delMessageById(@PathVariable("id") Integer id) {
		return messageService.delMessageById(id);
	}
	
	/**
	 * 编辑留言是否是回复状态
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editMessageIsAnswerById/{id}/{valid}")
	@ResponseBody
	public Boolean editMessageIsAnswerById(@PathVariable("id")Integer id,@PathVariable("valid")String valid) {
		Integer num = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			num = 1;
		}else {
			num = 0;
		}
		return messageService.editMessageIsAnswerById(id,num);
	}
	
	/**
	 * 编辑留言信息是否为公共的访问状态根据id
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editMessageIsPublicById/{id}/{valid}")
	@ResponseBody
	public Boolean editMessageIsPublicById(@PathVariable("id")Integer id,@PathVariable("valid")String valid) {
		Integer num = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			num = 1;
		}else {
			num = 0;
		}
		return messageService.editMessageIsPublicById(id,num);
	}
	
	
	/**
	 * 根据id来编辑留言页面的回复内容与是否回复状态
	 * @param id
	 * @param value
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editMessageContentById/{id}/{value}/{valid}")
	@ResponseBody
	public Boolean editMessageContentById(@PathVariable("id")Integer id,@PathVariable("value")String value,@PathVariable("valid")String valid) {
		Integer num = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			num = 1;
		}else {
			num = 0;
		}
		return messageService.editMessageContentById(id,value,num);
	}
	
	
}
